package com.zjjt.dc.tools.upload.file.util;

import com.zjjt.dc.common.util.OssBootUtil;
import com.zjjt.dc.tools.upload.file.config.AliOssBucketConfig;
import org.springframework.web.multipart.MultipartFile;

public class AliOssUtil {

    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;

    private AliOssUtil(String name){
        loadConfig(name);
    }

    private void loadConfig(String name) {
        AliOssBucketConfig aliOssBucketConfig = AliOssBucketConfig.newInstance(name);
        this.endpoint = aliOssBucketConfig.getEndpoint();
        this.accessKey = aliOssBucketConfig.getAccessKey();
        this.secretKey = aliOssBucketConfig.getSecretKey();
        this.bucketName = aliOssBucketConfig.getBucketName();
    }

    public static AliOssUtil newInstance(String name){
        return new AliOssUtil(name);
    }

    public String upload(MultipartFile file, String fileDir) throws Exception {
        OssBootUtil.setEndPoint(endpoint);
        OssBootUtil.setAccessKeyId(accessKey);
        OssBootUtil.setAccessKeySecret(secretKey);
        OssBootUtil.setBucketName(bucketName);
        return OssBootUtil.upload(file, fileDir);
    }
}
